package com.epoint.mobileoa.ui.mail;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebSettings.ZoomDensity;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.bizlogic.mobileoa.GetMailContentTask;
import com.epoint.mobileoa.bizlogic.mobileoa.Switch2TaskMail;
import com.epoint.mobileoa.datanalysis.XMLHelp;
import com.epoint.mobileoa.model.Attach;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.AlertHelp;
import com.epoint.mobileoa.tools.StringHelp;
import com.epoint.mobileoa.tools.ToastHelp;

/**
 * @author liyc
 * @time 2012-7-11 上午10:28:25
 * @annotation 邮件详情
 */
public class MailDetailView extends SuperView {

	public static int pagetype;//0邮件详情,1通知通告
	private WebView wv;
	private List<Attach> attachList = new ArrayList<Attach>();//附件列表
	private String saveguid;

	private String intenttype ;

	private int taskid;//1加载数据,2转任务邮件

	@Override
	public void initControls(){
		top_left_bt.setOnClickListener(this);
		
		intenttype = getIntent().getStringExtra("intenttype");
		intenttype = intenttype==null?"":intenttype;

		wv = (WebView) findViewById(R.id.md_wv);
		wv.getSettings().setJavaScriptEnabled(true);//支持JS
		wv.setBackgroundDrawable(getResources().getDrawable(R.drawable.white));
		wv.getSettings().setBuiltInZoomControls(true); //显示放大缩小 controler
		wv.getSettings().setSupportZoom(true); //可以缩放
		wv.getSettings().setDefaultZoom(ZoomDensity.MEDIUM);//默认缩放模式
		top_right_bt.setOnClickListener(this);

		taskid = 1;
		top_pb.setVisibility(View.VISIBLE);
		String pagetitle = getIntent().getStringExtra("pagetitle");
		top_title.setText(pagetitle);
		String mailguid = getIntent().getStringExtra("mailguid");
		HashMap param=new HashMap();
		param.put("mailguid", mailguid);
		Task task = new GetMailContentTask(this, param);
		MainService.addTask(task);
		saveguid = mailguid;

	}

	private String mailsender;
	private String mailsenderguid;
	private String mailtitle;
	private String contents;
	private String mailtime;
	private String mailguid;
	private String hasattach;
	private String CliengGuid;
	private String neettaskmonitor;
	@Override
	public void refresh(Object... param) {
		if(param[0].toString().equals(MainService.NORMAL)){
			if(taskid==1){
				if(pagetype==0){
					mailguid = getIntent().getStringExtra("mailguid");
					mailtitle = getIntent().getStringExtra("mailtitle");
					mailsender = getIntent().getStringExtra("mailsender");
					neettaskmonitor = StringHelp.getXMLAtt(param[1].toString(), "neettaskmonitor");
					CliengGuid = StringHelp.getXMLAtt(param[1].toString(), "CliengGuid");
					mailsenderguid = StringHelp.getXMLAtt(param[1].toString(), "mailsenderguid");
					mailtime = StringHelp.getXMLAtt(param[1].toString(), "sendtime");
					String zhusongnames = StringHelp.getXMLAtt(param[1].toString(), "zhusongnames");
					String chaosongnames = StringHelp.getXMLAtt(param[1].toString(), "chaosongnames");
					contents = StringHelp.getXMLAtt(param[1].toString(), "contents");
					//String contentImgIP = StringHelp.getXMLAtt(param[1].toString(), "contentImgIP");
					//String contents2 = contents.replaceAll("src=\"", "src=\""+contentImgIP).replaceAll("<a href=\"", "<a href=\""+contentImgIP);

					String fontcolor = "black";
					
					String html = "<table width='100%' cellpadding='0' cellspacing='0' border='0'><tr><td><font color="+fontcolor+" size=2>主题："+mailtitle+"</font></td></tr><tr ><td><hr/></td></tr><tr><td><font color="+fontcolor+" size=2>发送时间："+mailtime+"</font></td></tr><tr ><td><hr/></td></tr><tr><td><font color="+fontcolor+" size=2>发信人："+mailsender+"</font></td></tr><tr ><td><hr/></td></tr><tr><td><font color="+fontcolor+" size=2>收件人："+zhusongnames+"</font></td></tr><tr ><td><hr/></td></tr>";
					if(!chaosongnames.trim().equals("")){
						html += "<tr><td><font color="+fontcolor+" size=2>抄送人："+chaosongnames+"</font></td></tr><tr ><td><hr/></td></tr>";
					}
					html += "<tr><td>"+contents+"</td></tr>";
					html += "</table>";
					wv.loadDataWithBaseURL(null, html,"text/html","utf-8",null);
					String attaches = StringHelp.getXMLAttOut(param[1].toString(), "attaches");
					attachList = XMLHelp.AttachListAna(attaches);
					hasattach = getIntent().getStringExtra("hasattach");
					if(hasattach.equals("1")){
						top_right_bt.setVisibility(View.VISIBLE);
//						top_right_bt.setBackgroundResource(R.drawable.ic_btn_attach_selector"));
						top_right_bt.setText("附件");
					}
				}
			}
			else if(taskid==2){
				String bs = param[1].toString();
				if(bs.contains("<result>success</result>")){
					neettaskmonitor = "1";
					ToastHelp.showToast(this, "该邮件已转为任务邮件");
				}
			}
		}
		top_pb.setVisibility(View.INVISIBLE);
	}

	@Override
	public void onClick(View v) {
		if(v==top_right_bt){
			AlertHelp.showAttachDownloadAlert(this, attachList, saveguid);
		}
		else if(v==top_left_bt){
			finished();
		}
	}

	@Override
	public int getLayoutInt() {
		return R.layout.maildetail;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		pagetype = 0;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if(intenttype.equals("sjx")){
			menu.add(0, 1, 1, "回复").setIcon(R.drawable.img_mail_reply);
			menu.add(0, 2, 2, "转发").setIcon(R.drawable.img_mail_zf);
			if(!neettaskmonitor.equals("1")){
				menu.add(0, 3, 3, "转任务邮件").setIcon(R.drawable.menu_edit);
			}

		}else{
            menu.add(0, 1, 1, "转发").setIcon(R.drawable.img_mail_zf);
        }
		return super.onCreateOptionsMenu(menu);
	}

	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if(intenttype.equals("sjx")){
			switch(item.getItemId()){
			case 1:
				Intent replyIntent = new Intent(this,MailSendView.class);

				replyIntent.putExtra("mailsender", mailsender);
				replyIntent.putExtra("mailsenderguid", mailsenderguid);
				replyIntent.putExtra("replaytitle", mailtitle);
				replyIntent.putExtra("mailcontent", contents);
				replyIntent.putExtra("pagetype", 2);
				replyIntent.putExtra("mailtime", mailtime);

				startActivity(replyIntent);
				break;
			case 2:
				Intent zfIntent = new Intent(this,MailSendView.class);
				zfIntent.putExtra("replaytitle",mailtitle);
				zfIntent.putExtra("mailcontent", contents);
				zfIntent.putExtra("pagetype", 3);
				zfIntent.putExtra("mailtime", mailtime);
				zfIntent.putExtra("mailsender", mailsender);
				zfIntent.putExtra("mailsenderguid", mailsenderguid);
				zfIntent.putExtra("CliengGuid", CliengGuid);
				zfIntent.putExtra("hasattach", hasattach);
				startActivity(zfIntent);
				break;
			case 3:
				showRightProgressBar();
				taskid = 2;
				HashMap<String, Object> param = new HashMap<String, Object>();
				param.put("mailguid", mailguid);
				Task task = new Switch2TaskMail(this, param);
				MainService.addTask(task);
				break;
			}
		}else{
            switch(item.getItemId()){
                case 1:
                    Intent zfIntent = new Intent(this,MailSendView.class);
                    zfIntent.putExtra("replaytitle",mailtitle);
                    zfIntent.putExtra("mailcontent", contents);
                    zfIntent.putExtra("pagetype", 3);
                    zfIntent.putExtra("mailtime", mailtime);
                    zfIntent.putExtra("mailsender", mailsender);
                    zfIntent.putExtra("mailsenderguid", mailsenderguid);
                    zfIntent.putExtra("CliengGuid", CliengGuid);
                    zfIntent.putExtra("hasattach", hasattach);
                    startActivity(zfIntent);
                    break;
            }
        }
	
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void KEYCODE_BACK() {
		finished();
	}
	
	private void finished(){
		Intent intent = new Intent(this,MailBoxReceiveView.class);   
		intent.putExtra("mailguid",mailguid);   
        setResult(RESULT_OK, intent);   
        finish();   
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}
}
